package com.dhcc.bpm.modules.bbs.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.bbs.entity.Topic;
import com.dhcc.bpm.modules.bbs.service.ITopicService;
import com.dhcc.bpm.modules.bbs.vo.TopicVO;
import com.dhcc.bpm.modules.system.entity.Role;
import com.dhcc.bpm.modules.system.service.IRoleService;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * @ClassName TopicController
 * @Description 论坛板块表 控制器
 * @Author daina
 * @Date 2020/3/6 15:55
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 **/
@RestController
@AllArgsConstructor
@RequestMapping("bpm-bbs/topic")
@Api(value = "论坛板块表", tags = "论坛板块接口")
public class TopicController extends BladeController {

	private ITopicService topicService;
	private IRoleService roleService;

	private ISerialNumberService serialNumberService;
	/**
	 * @Description 详情
	 * @Author daina
	 * @Date 15:58 2020/3/6
	 * @param topic
	 * @return org.springblade.core.tool.api.R<com.dhcc.bpm.modules.bbs.entity.Topic>
	 **/
	@ApiLog("查看论坛板块详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入topic")
	@Log(title = "查看论坛板块详情",businessType = BusinessType.SELECT)
	public R<Topic> detail(Topic topic) {
		Topic detail = topicService.getOne(Condition.getQueryWrapper(topic));
		return R.data(detail);
	}

	/**
	 * @Description 分页
	 * @Author daina
	 * @Date 15:57 2020/3/6
	 * @param topic
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.entity.Topic>>
	 **/
	@ApiLog("查询信息列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入topic")
	@Log(title = "信息列表",businessType = BusinessType.SELECT)
	public R<IPage<Topic>> list(@ApiIgnore @RequestParam Map<String, Object> topic, Query query) {
		IPage<Topic> pages = topicService.page(Condition.getPage(query), Condition.getQueryWrapper(topic,Topic.class));
		return R.data(pages);
	}

	/**
	 * @Description 自定义分页
	 * @Author daina
	 * @Date 15:57 2020/3/6
	 * @param topic
	 * @param query
	 * @return org.springblade.core.tool.api.R<com.baomidou.mybatisplus.core.metadata.IPage<com.dhcc.bpm.modules.bbs.vo.TopicVO>>
	 **/
	@ApiLog("查询信息列表")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入topic")
	@Log(title = "信息列表",businessType = BusinessType.SELECT)
	public R<IPage<TopicVO>> page(TopicVO topic, Query query) {
		IPage<TopicVO> pages = topicService.selectTopicPage(Condition.getPage(query), topic);
		return R.data(pages);
	}

	/**
	 * @Description 新增
	 * @Author daina
	 * @Date 15:57 2020/3/6
	 * @param topic
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("新增论坛板块信息")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入topic")
	@Log(title = "新增",businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody Topic topic) {
		return R.status(topicService.save(topic));
	}

	/**
	 * @Description 修改
	 * @Author daina
	 * @Date 15:57 2020/3/6
	 * @param topic
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("修改论坛板块信息")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入topic")
	@Log(title = "修改论坛板块信息",businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody Topic topic) {
		return R.status(topicService.updateById(topic));
	}

	/**
	 * @Description 新增或修改
	 * @Author daina
	 * @Date 15:57 2020/3/6
	 * @param topic
	 * @return org.springblade.core.tool.api.R
	 **/
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入topic")
	@Log(title = "新增或修改论坛板块信息",businessType = BusinessType.INSERT)
	@ApiLog("新增或修改论坛板块信息")
	public R submit(@Valid @RequestBody Topic topic) {
		if (topic.getId() == null){
			String topicCode = serialNumberService.generateSerialNumberByModelCode("opc_access_control");
			topic.setTopicCode(topicCode);
		}
		topic.setTopicName(topic.getTopicName().trim());
		try {
			return R.status(topicService.saveOrUpdate(topic));
		} catch (Exception e) {
			if (e.getMessage().contains("for key 'bbstopic_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该板块名称已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}
	}

	
	/**
	 * @Description 删除
	 * @Author daina
	 * @Date 15:57 2020/3/6
	 * @param ids
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("删除论坛板块信息")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "删除", notes = "传入ids")
	@Log(title = "删除论坛板块信息",businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
//		List<Long> topicIds = Func.toLongList(ids);
//		topicIds.forEach(topicId->{
//
//		});
		return R.status(topicService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * @Description 获取当前用户权限
	 * @Author daina
	 * @Date 15:57 2020/3/6
	 * @return org.springblade.core.tool.api.R
	 **/
	@ApiLog("获取当前用户权限")
	@GetMapping("/getTopicUserRole")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "获取当前用户权限")
	@Log(title = "获取当前用户权限",businessType = BusinessType.SELECT)
	public R getTopicUserRole() {
		BladeUser user =  AuthUtil.getUser();
		String[] roles = user.getRoleId().split(",");
		for (String roleId : roles ) {
			Role role = roleService.getById(roleId);
			if(role != null && (role.getRoleAlias().equals("administrator") || role.getRoleAlias().equals("admin"))){
				return R.data("1");
			}
		}
		return R.data("2");
	}

}
